package com.Q08;

import java.util.ArrayList;
import java.util.List;

/**
 * @version v1.0.0
 * 创建时间：2022/8/2 13:10
 */
public class Q08_09 {
    ArrayList<String> res = new ArrayList<>();
    int n ;
    public List<String> generateParenthesis(int n) {
        if (n <= 0) return res;
        this.n  = n;
        int len = n * 2;
        helper(len,new StringBuilder(),n,0);
        return res;
    }

    private void helper(int len,StringBuilder path,int left ,int right){
        if (len == 0 && left == 0 && right == 0) {
            res.add(path.toString());
            return;
        }
        if (left>0) {
            path.append("(");
            helper(len - 1, path,left-1,right+1);
            path.deleteCharAt(path.length() - 1);
        }
        if (right>0) {
            path.append(")");
            helper(len - 1, path,left,right-1);
            path.deleteCharAt(path.length() - 1);
        }
    }

    public static void main(String[] args) {
        List<String> strings = new Q08_09().generateParenthesis(3);
        strings.forEach(p -> System.out.println(p));
    }
}
